<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>贝格智能资讯系统</title>
    <link rel="stylesheet" type="text/css" href="css/css.css" />

    <script
            src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.4/angular.js"
            type="text/javascript">
    </script>
    <script src="tm.pagination.js"></script>
    <script src="//cdn.bootcss.com/angular-ui-bootstrap/0.13.0/ui-bootstrap-tpls.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-router/0.2.15/angular-ui-router.min.js"></script>
    <script type="text/javascript">
        /*<![CDATA[*/
        var app = angular.module('app', ['tm.pagination','ui.bootstrap']);

        app.controller('MainContentController', function ($rootScope, $scope, $modal, $http) {
            $scope.data = {};
            $scope.rows = [];
            $scope.pageNum = 1;

            $scope.count = 0;
            $scope.p_pernum = 10;
            //变量
            $scope.p_current = 1;
            $scope.p_all_page = 0;
            $scope.pages = [];
            _get($scope.p_current, $scope.p_pernum);
            //获取数据
            //初始化载入数据
            function _get(page, size) {
                $scope.pageNum =page;
                var pageNum = page - 1;
                $http({
                    url: '/findAll?perPage=10&pageNum=' + pageNum,
                    method: 'GET'
                }).then(function (result)  {
                    var rows = result.data;
                    if (rows.length > 0) {
                        $scope.count = rows[0].num;
                        $scope.p_all_page = Math.ceil($scope.count / $scope.p_pernum);

                    } else {
                        $scope.count = 0
                        $scope.p_all_page = 0;
                    }

                    $scope.p_current = page;

                    reloadPno();

                    for (var i in rows) {
                        var row = rows[i];

                        row.labelsList = rows[i].news;

                        row.images = 'images/' + row.id + ".jpg"
                        // row.labelsList = row.labelsList.substr(0,row.labelsList.indexOf('_')-1)
                        $scope.rows.push(row);
                    }

                });
            }

            $scope.go = function(data) {
                window.location.href='topics.html?topic='+data;


            }


            //首页
            $scope.p_index = function () {
                $scope.load_page(1);
            }
            //尾页
            $scope.p_last = function () {
                $scope.load_page($scope.p_all_page);
            }
            //加载某一页
            $scope.load_page = function (page) {
                _get(page, $scope.p_pernum);
            };

            //初始化页码
            function reloadPno() {
                $scope.pages = calculateIndexes($scope.p_current, $scope.p_all_page, 8);
            };

//分页算法
            function calculateIndexes(current, length, displayLength) {
                var indexes = [];
                var start = Math.round(current - displayLength / 2);
                var end = Math.round(current + displayLength / 2);
                if (start <= 1) {
                    start = 1;
                    end = start + displayLength - 1;
                    if (end >= length - 1) {
                        end = length - 1;
                    }
                }
                if (end >= length - 1) {
                    end = length;
                    start = end - displayLength + 1;
                    if (start <= 1) {
                        start = 1;
                    }
                }
                for (var i = start; i <= end; i++) {
                    indexes.push(i);
                }
                return indexes;
            };

            $scope.more =  function (){
               $scope.pageNum=$scope.pageNum+1;
                _get($scope.pageNum,10);
           }

            var calculateIndexes = function (current, length, displayLength) {
                var indexes = [];
                var start = Math.round(current - displayLength / 2);
                var end = Math.round(current + displayLength / 2);
                if (start <= 1) {
                    start = 1;
                    end = start + displayLength - 1;
                    if (end >= length - 1) {
                        end = length - 1;
                    }
                }
                if (end >= length - 1) {
                    end = length;
                    start = end - displayLength + 1;
                    if (start <= 1) {
                        start = 1;
                    }
                }
                for (var i = start; i <= end; i++) {
                    indexes.push(i);
                }
                return indexes;
            };

            //获取数据
            $scope.get = function (id) {
                $http({
                    url: '/get?id=' + id,
                    method: 'POST',
                }).success(function (data) {
                    for (var i in $scope.rows) {
                        var row = $scope.rows[i];
                        if (data.id == row.id) {
                            row.no = data.no;
                            row.date = data.date;
                            row.quantity = data.quantity;
                            return;
                        }
                    }
                    $scope.rows.push(data);
                });
            }
            //获取数据
            $scope.test = function (id) {
                window.location.href='detail.html?id='+id;

            }
            function getMyDate(str) {
                str = parseInt(str);
                if (str != "" || str != null) {
                    var oDate = new Date(str);
                    var oYear = oDate.getFullYear();
                    var oMonth = oDate.getMonth() + 1;
                    oMonth = oMonth >= 10 ? oMonth : '0' + oMonth;
                    var oDay = oDate.getDate();
                    oDay = oDay >= 10 ? oDay : '0' + oDay;
                    var theDate = oYear + "-" + oMonth + "-" + oDay;
                } else {
                    theDate = "";
                }
                return theDate
            };

        });
        app.controller('alertController', function ($scope, $modalInstance, data) {
            $scope.getNum = data;
        });



        /*]]>*/
    </script>
</head>
<body ng-app="app" ng-controller="MainContentController">
<div class="header">
    <h2><div class="url"><a href="indexs.html">欢迎登录智能资讯系统！</a></div></h2>
    <h1>贝格智能资讯系统</h1></div>


<div class="container">

    <div class="list" ng-repeat="row in rows">

        <ul>
            <h7><span>热度：<b>{{row.hot_score}}</b></span>
                <img src="{{row.pic}}"/></h7>
            <dl>

                <dt>
                    <span><a ng-click="go(row.id)">{{row.topic}}</a></span>
                    <div ng-repeat="theme in row.themes">
                        <div class="Label"><a href="themes.html?tag={{theme}}">{{theme}}</a></div>

                    </div>
                    </dt>
                <li ng-repeat="label in row.labelsList">
                    <h5><a ng-click="test(label.id)">{{label.tit}}</a></h5>

                </li>


            </dl>
        </ul>

    </div>
    <div class="page">
        <li><a ng-click="more()" ng-if="p_all_page>pageNum">查看更多</a></li>
    </div>
</div>
<div class="back_top"><a href="javascript:scroll(0,0)"><img src="images/top.png"  /></a></div>
</body>
</html>
